package com.ruoyi.tws.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.api.R;
import com.ruoyi.common.core.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.service.IOrderService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2022-02-08
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    private final IOrderService iOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize(hasPermi="tws:order:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, Order order) {
        return getTD(query, order);
    }

    /**
     * 查询待支付订单列表
     */
    @PreAuthorize(hasPermi="tws:order:list")
    @GetMapping("/paymentList")
    public TableDataInfo paymentList(Query query, Order order) {
        order.setStatus(DictConstants.ORDER_STATUS.YWC.getKey());  // 订单已完成
        order.setIsPay("0");  // 订单未支付
        return getTD(query, order);
    }

    /**
     * 查询待开票订单列表
     */
    @PreAuthorize(hasPermi="tws:order:list")
    @GetMapping("/billList")
    public TableDataInfo billList(Query query, Order order) {
        order.setStatus(DictConstants.ORDER_STATUS.YWC.getKey());  // 订单已完成
        order.setIsPay("1");  // 订单已支付
        order.setIsBill("0");  // 订单未开票
        return getTD(query, order);
    }

    private TableDataInfo getTD(Query query, Order order) {
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(order.getStatus())) {
            lqw.eq(Order::getStatus, order.getStatus());
        }
        if (StringUtils.isNotBlank(order.getShippingNoteNumber())) {
            lqw.eq(Order::getShippingNoteNumber, order.getShippingNoteNumber());
        }
        if (StringUtils.isNotBlank(order.getDriverPhone())) {
            lqw.like(Order::getDriverPhone, order.getDriverPhone());
        }
        if (StringUtils.isNotBlank(order.getDriverName())) {
            lqw.like(Order::getDriverName, order.getDriverName());
        }
        if (StringUtils.isNotBlank(order.getIsSendToPro())) {
            lqw.eq(Order::getIsSendToPro, order.getIsSendToPro());
        }
        if (StringUtils.isNotBlank(order.getIsPay())) {
            lqw.eq(Order::getIsPay, order.getIsPay());
        }
        if (StringUtils.isNotBlank(order.getIsBill())) {
            lqw.eq(Order::getIsBill, order.getIsBill());
        }
        lqw.orderByDesc(Order::getCreateTime);
        IPage<Order> list = iOrderService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize(hasPermi = "tws:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order) throws IOException
    {
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<Order>(order);
        List<Order> list = iOrderService.list(lqw);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order. class);
        util.exportExcel(response, list, "order");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize(hasPermi="tws:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iOrderService.getInfo(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize(hasPermi="tws:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        return toAjax(iOrderService.add(order));
    }

    /**
     * 修改订单
     */
    @PreAuthorize(hasPermi="tws:order:edit")
    @Log(title = "订单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(iOrderService.edit(order));
    }

    /**
     * 评价订单
     */
    @PreAuthorize(hasPermi="tws:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/starOrder")
    public AjaxResult star(@RequestBody Order order) {
        return toAjax(iOrderService.starOrder(order));
    }

    /**
     * 删除订单
     */
    @PreAuthorize(hasPermi="tws:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(iOrderService.delete(id));
    }

    /**
     * 查询当前登录货源方最近未完成订单进度
     */
    @GetMapping("/nearlyOrder")
    public R<List<Order>> nearlyOrder() {
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.in(Order::getStatus,
                DictConstants.ORDER_STATUS.DFC.getKey(),
                DictConstants.ORDER_STATUS.YSZ.getKey());
        lqw.orderByDesc(Order::getCreateTime);
        List<Order> list = iOrderService.list(lqw);
        return R.ok(list);
    }

    /**
     * 获取支付/开票相关订单
     */
    @PostMapping("/comOrder")
    public AjaxResult comOrder(@RequestParam String id,
                               @RequestParam Integer type){
        List<Order> list = new ArrayList<>();
        if (type == 1){
            list = iOrderService.getBillOrder(id);
        } else if (type == 2){
            list = iOrderService.getPayOrder(id);
        }
        return AjaxResult.success(list);
    }

}
